package com.example.mybus;

import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.example.mybus.dao.ViajeDao;
import com.example.mybus.model.Viaje;
import com.example.mybus.model.pmf.PMF;

public class MostrarViajesServlet extends HttpServlet {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final Logger log = Logger.getLogger("MostrarViajesServlet");

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		PersistenceManager pm = PMF.get().getPersistenceManager();

		try {
			List<Viaje> viajes = ViajeDao.getAll();
			req.setAttribute("viajes", viajes);
		} finally {
			pm.close();
		}
		
		req.getRequestDispatcher("/viaje/viajes.jsp").forward(req, resp);
		log.log(Level.INFO, "Redirect to viajes.jsp");
		
	}
	
}
